﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace StockPoint.Admin
{
    public class Paging
    {
        private const string ITEM = "<li>{0}</li>";
        private const string ITEM_LINK = "<a href=\"javascript:Paging('{0}');\">{1}</a>";
        private const string ITEM_CUR = "<li class=\"selected\">{0}</li>";

        private const string ITEM_PAGE_FIRST = "<li class=\"first\">{0}</li>";
        private const string ITEM_PAGE_PREV = "<li class=\"prev\">{0}</li>";
        private const string ITEM_PAGE_NEXT = "<li class=\"next\">{0}</li>";
        private const string ITEM_PAGE_LAST = "<li class=\"last\">{0}</li>";

        private const string IMG_PAGE_FIRST = "<img src=\"/Images/common/list_icon_paging_first.gif\" alt=\"<<\" />";
        private const string IMG_PAGE_PREV = "<img src=\"/Images/common/list_icon_paging_prev.gif\" alt=\"<\" />";
        private const string IMG_PAGE_NEXT = "<img src=\"/Images/common/list_icon_paging_next.gif\" alt=\">\" />";
        private const string IMG_PAGE_LAST = "<img src=\"/Images/common/list_icon_paging_last.gif\" alt=\">>\" />";

        public static string GetPaging(int iViewCount, int iTotalRecord, int iCurPage)
        {
            return GetPaging(iViewCount, iTotalRecord, iCurPage, ITEM, ITEM_CUR, IMG_PAGE_FIRST, IMG_PAGE_LAST, ITEM_PAGE_PREV, ITEM_PAGE_NEXT);
        }

        public static string GetPaging(int iViewCount, int iTotalRecord, int iCurPage, string sItem
            , string sItemCur, string sItemPageFirst, string sItemPageLast, string sItemPagePrev, string sItemPageNext)
        {
            // Calculate from, to page.
            string sFirst, sPrev, sPage, sNext, sLast;
            int iFromPage, iToPage;

            int iPageCount = (iTotalRecord / iViewCount);
            if (iTotalRecord % iViewCount != 0) iPageCount++;

            iFromPage = (Convert.ToInt32((iCurPage - 1) / 10) * 10) + 1;
            if (iPageCount > iFromPage + 9)
                iToPage = iFromPage + 9;
            else
                iToPage = iPageCount;

            // First, Previous page
            if (iCurPage > 1)
            {
                sFirst = string.Format(ITEM_PAGE_FIRST, string.Format(ITEM_LINK, 1, IMG_PAGE_FIRST));
                sPrev = string.Format(ITEM_PAGE_PREV, string.Format(ITEM_LINK, (iCurPage - 1), IMG_PAGE_PREV));
            }
            else
            {
                sFirst = string.Format(ITEM_PAGE_FIRST, IMG_PAGE_FIRST);
                sPrev = string.Format(ITEM_PAGE_PREV, IMG_PAGE_PREV);
            }

            // Pages
            sPage = string.Empty;
            for (int i = iFromPage; i <= iToPage; i++)
            {
                if (i == iCurPage)
                    sPage += string.Format(ITEM_CUR, i);
                else
                    sPage += string.Format(ITEM, string.Format(ITEM_LINK, i, i));
            }

            // Next, Last page
            if (iCurPage < iPageCount)
            {
                sNext = string.Format(ITEM_PAGE_NEXT, string.Format(ITEM_LINK, (iCurPage + 1), IMG_PAGE_NEXT));
                sLast = string.Format(ITEM_PAGE_LAST, string.Format(ITEM_LINK, iPageCount, IMG_PAGE_LAST));
            }
            else
            {
                sNext = string.Format(ITEM_PAGE_NEXT, IMG_PAGE_NEXT);
                sLast = string.Format(ITEM_PAGE_LAST, IMG_PAGE_LAST);
            }

            return "<ul>" + sFirst + sPrev + sPage + sNext + sLast + "</ul>";
        }

    }
}